package cz.data.common.database.metadata.parser;

import cz.data.common.exception.DataException;
import cz.data.common.support.metadata.PostgresqlDataTypeEnum;
import cz.data.common.support.ColumnParser;
import cz.data.common.support.DataType;
import cz.data.common.support.DataTypeEnum;

import java.util.Optional;

public class PostgresqlColumnParser implements ColumnParser {

    @Override
    public DataType parse(DataTypeEnum dataTypeEnum) {
        PostgresqlDataTypeEnum typeEnum = PostgresqlDataTypeEnum.match(dataTypeEnum.getValue(), PostgresqlDataTypeEnum.VARCHAR);
        switch (typeEnum) {
            case CHAR:
                return new CharDataType();
            case VARCHAR:
                return new VarcharDataType();
            case TEXT:
                return new TextDataType();
            case SMALLINT:
                return new SmallintDataType();
            case INTEGER:
                return new IntegerDataType();
            case NUMERIC:
                return new NumericDataType();
            case DATE:
                return new DateDataType();
            case TIME:
                return new TimeDataType();
            case DATETIME:
            case TIMESTAMP:
                return new TimestampDataType();
            case BOOLEAN:
                return new BooleanDataType();
            case ARRAY_VARCHAR:
                return new ArrayVarcharDataType();
            case BYTEA:
                return new ByteaDataType();
            default:
                throw new DataException("不支持的类型");
        }
    }

    public static class CharDataType implements DataType {
        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "CHAR(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class VarcharDataType implements DataType {
        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "VARCHAR(" + Optional.ofNullable(columnLength).orElse("255") + ")";
        }
    }

    public static class TextDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TEXT";
        }
    }

    public static class SmallintDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "SMALLINT";
        }
    }

    public static class IntegerDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "INTEGER";
        }
    }

    public static class NumericDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "NUMERIC";
        }
    }

    public static class DateDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "DATE";
        }
    }

    public static class TimeDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TIME";
        }
    }

    public static class TimestampDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "TIMESTAMP";
        }
    }

    public static class BooleanDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "BOOLEAN";
        }
    }

    public static class ArrayVarcharDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "_VARCHAR";
        }
    }

    public static class ByteaDataType implements DataType {

        @Override
        public String fillTypeString(String columnLength, String columnScale) {
            return "BYTEA";
        }
    }

}
